<template>
    <div class="login-bg">
        <div class="login-container">
            <div class="login-header">
                <img class="logo mr10" src="../../assets/img/logo.svg" alt="" />
                <div class="login-title">宠物领养后台管理系统</div>
            </div>
            <el-form :model="param" :rules="rules" ref="login" size="large">
                <el-form-item prop="admin_email">
                    <el-input v-model="param.admin_email" placeholder="账号/邮箱">
                        <template #prepend>
                            <el-icon>
                                <User />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="admin_pwd">
                    <el-input
                        type="admin_pwd"
                        placeholder="密码"
                        v-model="param.admin_pwd"
                        @keyup.enter="submitForm(login)"
                    >
                        <template #prepend>
                            <el-icon>
                                <Lock />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <div class="pwd-tips">
                    <el-checkbox class="pwd-checkbox" v-model="checked" label="记住密码" />
                    <!-- <el-link type="primary" @click="$router.push('/reset-pwd')">忘记密码</el-link> -->
                </div>
                <el-button class="login-btn" type="primary" size="large" @click="submitForm(login)">登录</el-button>
                <!-- <p class="login-tips">Tips : 管理员账号联系负责人注册。</p> -->
                <!-- <p class="login-text">
                    没有账号？<el-link type="primary" @click="$router.push('/register')">立即注册</el-link>
                </p> -->
            </el-form>
        </div>
    </div>
</template>

<script setup lang="ts">

import { ref, reactive } from 'vue';

import { useTabsStore } from '@/store/tabs';

import { usePermissStore } from '@/store/permiss';

import { useRouter } from 'vue-router';

import { ElMessage } from 'element-plus';

import type { FormInstance, FormRules } from 'element-plus';

import { loginAPI } from '../../api/index';


// 规范表单信息对象
interface LoginInfo {
    admin_email: string;
    admin_pwd: string;
}

const lgStr = localStorage.getItem('login-param');
const defParam = lgStr ? JSON.parse(lgStr) : null;
const checked = ref(lgStr ? true : false);

const router = useRouter();
// 用户记录表单信息的对象
const param = reactive<LoginInfo>({
    admin_email: defParam ? defParam.admin_email : '',
    admin_pwd: defParam ? defParam.admin_pwd : '',
});

// 判断表单数据是否存在或符合格式
const rules: FormRules = {
    admin_email: [
        {
            required: true,
            message: '请输入账号/邮箱',
            trigger: 'blur',
        },
    ],
    admin_pwd: [{ required: true, message: '请输入密码', trigger: 'blur' }],
};

const permiss = usePermissStore();

const login = ref<FormInstance>();

// 提交表单
const submitForm = (formEl: FormInstance | undefined) => {
    // 检查表单信息
    // console.log(param);

    if (!formEl) return;
    formEl.validate( async (valid: boolean) => {
        
        if (valid) {
            // 解构赋值 获取表单信息
            let { admin_email ,admin_pwd } = param;
            // 调用登录接口
            await loginAPI({ admin_email ,admin_pwd })
            .then(
                content=>{
                    console.log("登录成功",content)
                    let {code , msg , token } = content;
                    if(code == 200) {
                         ElMessage.success(msg);
                        //把账号存储在本地
                         localStorage.setItem('ms_admin_email', param.admin_email);
                        //设置权限
                        const keys = permiss.defaultList['admin'];
                        permiss.handleSet(keys);
                        localStorage.setItem('ms_keys', JSON.stringify(keys));

                        // 存储token
                        localStorage.setItem("AUTO_TOKEN",token);

                        //延迟跳转首页
                        setTimeout(()=>{
                            // router.push('/');
                            // console.log("test")
                            router.push({path:'/dashboard'})
                        },300)


                        // 把表单信息存储在本地
                        // if (checked.value) {
                        //     localStorage.setItem('login-param', JSON.stringify(param));
                        // } else {
                        //     localStorage.removeItem('login-param');
                        // }
                    }
                    else {
                        ElMessage.error(msg);
                    }
                },
                error=>{
                    console.log("登录失败",error)
                }
            )

           
            
           
           
           
          
            
        } else {
            ElMessage.error('登录失败');
            return false;
        }
    });

    return false;
};

const tabs = useTabsStore();
tabs.clearTabs();
</script>

<style scoped>
.login-bg {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    background: url(../../assets/img/login-bg.jpg) center/cover no-repeat;
}

.login-header {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 40px;
}

.logo {
    width: 35px;
}

.login-title {
    font-size: 22px;
    color: #333;
    font-weight: bold;
}

.login-container {
    width: 450px;
    border-radius: 5px;
    background: #fff;
    padding: 40px 50px 50px;
    box-sizing: border-box;
}

.pwd-tips {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 14px;
    margin: -10px 0 10px;
    color: #787878;
}

.pwd-checkbox {
    height: auto;
}

.login-btn {
    display: block;
    width: 100%;
}

.login-tips {
    font-size: 12px;
    color: #999;
}

.login-text {
    display: flex;
    align-items: center;
    margin-top: 20px;
    font-size: 14px;
    color: #787878;
}
</style>
